<?php

namespace Xuan\Controller;

class IndexController extends CommonController {

    public function index() {


        $this->display();
    }

    public function info() {
        $config = M("config")->where("id = 1")->find();
        $citys = M("area")->where("pid = 0")->select();
        $this->assign("citys", $citys);
        $this->assign("config", $config);
        $this->display("Index:info");
    }

    public function info_post() {
        $path = C('picFile');
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->maxSize = C('upload_max_size'); // 设置附件上传大小
        $upload->rootPath = $path; // 设置附件上传目录
        $upload->saveName = array('uniqid', '');
        $upload->autoSub = false;
        $info = $upload->upload();
        if ($info) {
            $uploadInfo = $info['logo'];
            $data['logo'] = $uploadInfo['savename']; //数据库保存名
        }
        $cityRadio = I("post.cityRadio");
        if ($cityRadio == 1) {
            $data['city'] = I("post.city");
        } else {
            $data['city'] = 0;
        }
        $data['ctitle'] = I('post.ctitle');
        $data['title'] = I('post.title');
        $data['keywords'] = I('post.keywords');
        $data['description'] = I('post.description');
        $data['points_comment'] = I('post.points_comment');
        $data['copyright'] = I('post.copyright');
        $data['points_daysign'] = I('post.points_daysign');
        $data['points_monthsign'] = I('post.points_monthsign');
        $data['points_invite'] = I('post.points_invite');
        $data['statics'] = I('post.statics');
        $data['cart_type'] = I('post.cart_type');
        M("config")->where("id = 1")->save($data);
//        if ($data['logo'] != '') {
//            include("common/qrcode.php");
//            getQrcode($data['wechatNum'], $path . $data['logo'], "Public/images/wechat.jpg");
//        }
        clearTempFile();
        $this->success('修改成功！', U('Index/info'));
    }

    public function pwd_post() {
        $Dao = M("admin_user");
        $info = $Dao->where("id = " . session('admin_uid') . " AND pwd = '" . I('post.pwd_old') . "'");
        if ($info) {
            $data['pwd'] = md5(I('post.pwd'));
            $Dao->where("id = " . session('admin_uid') . "")->save($data);
            $this->success('修改成功！', U('Index/pwd'));
        } else {
            $this->success('原密码错误！', U('Index/pwd'));
        }
    }

    public function email() {
        $info = M("three")->where("id = 1")->find();
        $detail = json_decode($info['emails'], true);
        $this->assign("detail", $detail);
        $this->display();
    }

    public function email_post() {
        $arr = array();
        $arr['smpt'] = I('post.smpt');
        $arr['email'] = I('post.email');
        $arr['pwd'] = I('post.pwd');
        $data['emails'] = json_encode($arr);
        M("three")->where("id = 1")->save($data);
        clearTempFile();
        $this->success('修改成功！', U('Index/email'));
    }

    public function menu_detail() {
        $id = I('get.id', '0', 'int');
        if($id>0){
        $detail = M("admin_menu2")->where("id =" . $id . "")->find();
        }else{
            $detail['is_check'] =1;
        }
        $this->assign("detail", $detail);
        $this->display();
    }

    public function menu_detail_post() {
        $id = I('post.id', '', 'int');
        $data['name'] = I('post.name');
        $data['pid'] = I('post.pid', 0, 'int');
        $data['control'] = I('post.control');
        $data['mod'] = I('post.mod');
        $data['icon'] = I('post.icon');
        $data['is_check'] = I('post.is_check', '0', 'int');
        $data['ord'] = I('post.ord', '30', 'int');
        if ($id > 0) {
            M("admin_menu2")->where("id = " . $id . "")->save($data);
            $this->success('修改成功！', U('Index/menus'));
        } else {
            M("admin_menu2")->add($data);
            $this->success('添加成功！', U('Index/menus'));
        }
    }
     public function managers() {
        $sql = "id != 1";
        $lists = M('admin_user')->where($sql)->order('id DESC')->select();
        $this->assign("lists", $lists);
        $this->display();
    }

    public function managers_detail() {
        $id = I('get.id', '', 'int');
        $detail = M("admin_user")->where("id = " . $id . "")->find();
  
//        $lists = M("admin_menu")->where("pid = 0 AND is_show = 1")->order("ord ASC,id ASC")->select();
//        foreach($lists as $k=>$v){
//            $lists[$k]['sub'] = M("admin_menu")->where("pid = ".$v['id']." AND is_show = 1")->order("ord ASC,id ASC")->select();
//            foreach($lists[$k]['sub'] as $k2=>$v2){
//            $lists[$k]['sub'][$k2]['sub2'] = M("admin_menu")->where("pid = ".$v2['id']." AND (is_show = 1)")->order("ord ASC,id ASC")->select();
//        }
//        }
//        $this->assign("lists", $lists);
        $this->assign("detail", $detail);
        
        $this->display();
    }

    public function managers_detail_post() {

        $id = I('post.id', '', 'int');
        $data['name'] = I('post.name');
        if(I('post.pwd')){
            $data['pwd'] = md5(I('post.pwd'));
        }
         $power = I("post.power");
        if($power){
            $data['action'] = implode(",", $power);
            
        }else{
            $data['action'] = '';
        }
        if ($id > 0) {
            M("admin_user")->where("id = " . $id . "")->save($data);
            $this->success('修改成功！',  U('Index/managers'));
        } else {
            M("admin_user")->add($data);
            $this->success('添加成功！', U('Index/managers'));
        }
    }
    public function say() {
        $sql = "1=1 AND pid = 0";
        
        $keywords = trim(I('get.keywords'));
        if ($keywords) {
            $sql .= " AND content like '%" . $keywords . "%'";
        }
        $count = M("say")->where($sql)->count();    //计算总数
        $Page = new \Think\Page($count,C("pagenum"));
        $lists = M("say")->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        foreach($lists as $k=>$v){
          $lists[$k]['num'] = M("say")->where("pid = ".$v['id']."")->count();
        }
       // echo $Dao->getLastSql();
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
        $this->assign("keywords", $keywords);
        $this->display();
    }

}
